﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HotelManager.DataAccess.Abstract;
using HotelManager.DataAccess.Helpers;
using HotelManager.DataAccess.Model;

namespace HotelManager.DataAccess.Concrete
{
    public class HotelRepository : IHotelRepository
    {
        public IQueryable<Hotel> Hotels
        {
            get
            {
                using (var context = new HotelDbEntities())
                {
                    return context.Hotels;
                }
            }
        }

        public List<Hotel> SearchHotel(SearchModel model)
        {
            using (var context = new HotelDbEntities())
            {
                //LiemDT test list
                var list = context.HotelRooms
                    .Include(x => x.RoomType.Hotel.HotelOptions)
                    .Where(x => x.AvailableTime == model.CheckInTime
                                && x.Quantity > 0
                                && x.RoomType.Hotel.CountryId == model.CountryId
                                && x.RoomType.Hotel.CityId == model.CityId
                                && x.RoomType.Hotel.IsActive)
                    .Select(x => x.RoomType.Hotel)
                    .Include(x => x.HotelOptions)
                    .Include(x => x.RoomTypes)
                    .ToList();
                //var list = context.HotelRooms
                //    .Select(x => x.RoomType.Hotel)
                //    .Include(x => x.HotelOptions)
                //    .Include(x => x.RoomTypes)
                //    .ToList();
                //
                list = list.Distinct(new HotelComparer()).ToList();
                return list;
            }
        }

        public HotelInfosModel GetHotelDescription(int hotelId)
        {
            XmlHelper helper = new XmlHelper();
            return helper.GetHotelDescription(hotelId);
        }

        //Liemdt - Add
        public List<HotelOption> GetAllHotelOptions()
        {
            using (var context = new HotelDbEntities())
            {
                return context.HotelOptions.ToList();
            }
        }


        public List<Option> GetAllOptions()
        {
            using (var context =new HotelDbEntities())
            {
                return context.Options.ToList();
            }
        }


        public bool BookingHotelRoom(Booking book)
        {
            try
            {
                using (var context = new HotelDbEntities())
                {
                    //Save booking information
                    context.Bookings.Add(book);
                    context.SaveChanges();

                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }
            
        }

        public BookingRoomInfo GetRoomInfo(int id, DateTime checkIn)
        {
            try
            {
                using (var context = new HotelDbEntities())
                {
                    BookingRoomInfo info = (from roomType in context.RoomTypes
                                            from hotel in context.Hotels
                                            from city in context.Cities
                                            from country in context.Countries
                                            where roomType.Id == id
                                                && hotel.Id == roomType.HotelId
                                                && city.Id == hotel.CityId
                                                && country.Id == hotel.CountryId
                                            select new BookingRoomInfo
                                            {
                                                RoomTypeId = roomType.Id,
                                                RoomTypeName = roomType.Name,
                                                Price = roomType.Price,
                                                HotelName = hotel.Name,
                                                Stars = hotel.Stars.Value,
                                                City = city.Name,
                                                Country = country.Name
                                            }).Single();

                    int rooms = (from hotelRoom in context.HotelRooms
                                 where hotelRoom.Id == id
                                 && hotelRoom.AvailableTime.Year == checkIn.Year
                                 && hotelRoom.AvailableTime.Month == checkIn.Month
                                 && hotelRoom.AvailableTime.Day == checkIn.Day
                                 select hotelRoom.Quantity).Single();

                    info.AvailableQuantity = rooms;

                    return info;
                }
            }
            catch (Exception)
            {
                return null;
            }
        }


        
    }
}
